<template>
  <div class="source-order-select">
    <el-divider style="margin: 10px 0" />

    <!-- 搜索表单 -->
    <el-form :model="searchForm" inline>
      <el-form-item label="单据编号：">
        <el-input v-model="searchForm.procurement_code" placeholder="请输入单据编号" style="width: 200px;" />
      </el-form-item>
      <el-form-item label="供应商：">
        <el-input v-model="searchForm.supplier_name" placeholder="请输入供应商" style="width: 200px;" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table :data="orderList" border height="300px" @selection-change="handleSelectionChange" v-loading="loading">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column prop="supplier_name" label="供应商" width="200" />
      <el-table-column prop="procurement_code" label="单据编号" width="200" />
      <el-table-column prop="procurement_date" label="单据日期" width="160" />
    </el-table>

    <div style="margin-top: 10px; font-size: 14px; color: #606266;">
      已选中 {{ selectedCount }} 项
    </div>

    <!-- 分页 -->
    <div class="page-div" v-if="procurementPageInfo.total > 0">
      <el-pagination :current-page="procurementPageInfo.pageNum" :page-size="procurementPageInfo.pageSize"
        :total="procurementPageInfo.total" @current-change="handlePageChange" layout="total, ->, prev, pager, next, jumper"
        background />
    </div>
  </div>
</template>

<script setup>
import { ref, computed, inject, onMounted, defineExpose } from 'vue'
import { ElMessage } from 'element-plus'

const axios = inject('axios')


// 搜索表单
const searchForm = ref({
  procurementOrder_code: '',
  supplier_name: '',
  order_startDate: '',
  order_endDate: ''
})


// 分页
const procurementPageInfo = ref({
  pageNum: 1,
  pageSize: 10,
  total: 0
})

// 数据与状态
const orderList = ref([{}])
const loading = ref(false)
const selectedItems = ref([])

// 计算属性
const selectedCount = computed(() => selectedItems.value.length)


// 搜索方法（可被父组件调用）
const search = () => {
  procurementPageInfo.value.pageNum = 1
  loadOrders()
}

// 分页切换
const handlePageChange = (pageNum) => {
  procurementPageInfo.value.pageNum = pageNum
  loadOrders()
}

// 表格选择变化
const handleSelectionChange = (selection) => {
  selectedItems.value = selection
}

// 加载数据
const loadOrders = async () => {
  if (loading.value) return;
  loading.value = true
  try {
    const res = await axios.get('/lxw/selectMainProcurementPage', {
      params: {
        pageNum: procurementPageInfo.value.pageNum,
        pageSize: procurementPageInfo.value.pageSize,
        isRefund: 0,
        ...searchForm
      }
    })

    if (res.data.code == 5200) {
      orderList.value = res.data.procurementPageInfo?.list || []
      procurementPageInfo.value.total = res.data.procurementPageInfo?.total || 0
      console.log("orderList: ", orderList.value)
    } else {
      ElMessage.warning(res.data.msg || '加载失败')
      orderList.value = []
      procurementPageInfo.value.total = 0
    }
  } catch (error) {
    console.error('请求异常:', error)
    ElMessage.error('网络错误，请重试')
    orderList.value = []
    procurementPageInfo.value.total = 0
  } finally {
    loading.value = false
  }
}

const getSelectedItems = () => {
  return selectedItems.value
}
const resetSearch = () => {
    Object.keys(searchForm).forEach(key => {
      searchForm[key] = ''
    })
  }
// 暴露给父组件的方法
defineExpose({
  getSelectedItems,
  resetSearch,
})


onMounted(() => {
  loadOrders();
})
</script>

<style scoped>
.source-order-select {
  padding: 10px;
}

.page-div {
  margin-top: 10px;
  text-align: center;
}
</style>